package com.sihai.ehr.web.action.sign;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.*;
import com.sihai.ehr.model.sys.AuditStatus;
import com.sihai.ehr.model.sys.User;
import com.sihai.ehr.service.AdministrationEvectionReportFormService;
import com.sihai.ehr.service.UserService;
import com.sihai.ehr.utils.DateUtil;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wxn
 * @since 13-4-15 21:22
 *
 * 行政签核系统 - 出差报告单Action
 */
@Results({
        @Result(name = "list", location = "/WEB-INF/pages/sign/administration_evection_report_form_list.jsp"),
        @Result(name = "add", location = "/WEB-INF/pages/sign/administration_evection_report_form_input.jsp"),
        @Result(name = "audit", location = "administration_evection_report_form_audit.jsp")
})
public class AdministrationEvectionReportFormAction extends SignBaseAction {
    @BeanInject
    private AdministrationEvectionReportFormService administrationEvectionReportFormService;
    @BeanInject
    private UserService userService;

    private LimitedList<AdministrationEvectionReportForm> reportForms;
    private AdministrationEvectionReportForm reportForm;
    private List<AdministrationEvectionReportFormDetail> formDetails;

    private List<List<AdministrationEvectionReportFormAuditing>> audits;

    @Action("/public/administration-evection-report-form")
    public String listByUser() throws Exception {
        userType = 2;
        String temp = execute();
        showChildOrg = false;
        return temp;
    }

    @Action("/sign/administration-evection-report-form")
    @Override
    public String execute() throws Exception {
        showChildOrg = true;
        init();
        User user = null;
        if(userType == 2){
            user = userService.getUser(getCurrentUserId());
        }
        reportForms = administrationEvectionReportFormService.findAdministrationEvectionReportForm(user, searchFormPojo, createLimit(), createSorter());
        this.paging(reportForms);
        this.setPageUrl(userType == 1 ? "sign/administration-evection-report-form.xhtml" : "public/administration-evection-report-form.xhtml");
        return "list";
    }

    @Action("/public/administration-evection-report-form-input")
    public String toAddByUser() throws Exception {
        userType = 2;
        return "add";
    }
    @Action("/sign/administration-evection-report-form-input")
    public String toAdd() throws Exception {
        return "add";
    }

    @Action("/public/administration-evection-report-form-save")
    public String saveByUser() throws Exception {
        userType = 2;
        return save();
    }

    //保存
    @Action("/sign/administration-evection-report-form-save")
    public String save() throws Exception {
        String[] detailDates = getParameterValues("detail_date_input");
        String[] detailAddress = getParameterValues("detail_address_input");
        String[] detailTargetrs = getParameterValues("detail_target_input");
        String[] detailContents = getParameterValues("detail_content_input");
        if(reportForm == null || detailDates == null ||  detailAddress == null || detailTargetrs == null || detailContents == null){
            return execute();
        }
        int length = detailDates.length;
        List<AdministrationEvectionReportFormDetail> details = new ArrayList<AdministrationEvectionReportFormDetail>(length);
        for(int i=0; i<length; i++){
            AdministrationEvectionReportFormDetail detail = new AdministrationEvectionReportFormDetail();
            detail.setDateTiem(DateUtil.createDate(detailDates[i], "yyyy-MM-dd"));
            detail.setAddress(detailAddress[i]);
            detail.setTarget(detailTargetrs[i]);
            detail.setContent(detailContents[i]);
            details.add(detail);
        }

        Long userId = getCurrentUserId();
        EmployeeinfoWork work = null;
        if(userId > 0){
            User user = userService.getUser(userId);
            if(user != null){
                work = user.getWork();
            }
        }
        reportForm.setStatus(AuditStatus.wait_audit);
        reportForm.setWork(work);
        administrationEvectionReportFormService.save(reportForm, details);
        return execute();
    }

    @Action("/sign/administration-evection-report-form-audit")
    public String toAudit() throws Exception {
        //得到审核对象
        if(reportForm == null || reportForm.getId() == null){
            return execute();
        }
        reportForm = administrationEvectionReportFormService.get(reportForm.getId());
        formDetails = administrationEvectionReportFormService.findAdministrationEvectionReportFormDetails(reportForm);

        //得到当前审核历史记录(没有就先保存一个审核流程次数)
        audits = administrationEvectionReportFormService.findAdministrationEvectionReportFormAuditing(reportForm.getId());
        if(audits == null || audits.isEmpty()){
            administrationEvectionReportFormService.saveAdministrationEvectionReportFormAuditingProcess(reportForm.getId());
            audits = administrationEvectionReportFormService.findAdministrationEvectionReportFormAuditing(reportForm.getId());
        }
        //检查当前登录用户是否可以审核当前结点
        if(reportForm.getWork() != null && audits != null && !audits.isEmpty()){
            checkCanAudit(reportForm.getWork().getDuty(), audits.get(audits.size()-1));
        }
        currentAuditType = AuditType.audit_administration_evection_report_form.toString();

        return "audit";
    }

    public LimitedList<AdministrationEvectionReportForm> getReportForms() {
        return reportForms;
    }

    public AdministrationEvectionReportForm getReportForm() {
        return reportForm;
    }

    public void setReportForm(AdministrationEvectionReportForm reportForm) {
        this.reportForm = reportForm;
    }

    public List<List<AdministrationEvectionReportFormAuditing>> getAudits() {
        return audits;
    }

    public List<AdministrationEvectionReportFormDetail> getFormDetails() {
        return formDetails;
    }
}
